package com.example.myapplication.lawyer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Toast;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;

import org.json.JSONException;
import org.json.JSONObject;

public class ServicecommenttActivity extends AppCompatActivity {

    private String serviceID;
    private ImageView but_topchoicelawyy;
    private RatingBar ratingBar;
    private EditText ed_servicecommentt;
    private LinearLayout li_submit;
    private  String TAG = "TAG" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_servicecommentt);
        initview();
        //返回
        but_bloak();
        //获取评论数据
        commentdata();
    }


    private void commentdata() {
        li_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tonten_servicecommentt = ed_servicecommentt.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id",serviceID);
                    jsonObject.put("evaluate",tonten_servicecommentt);
                    //评分[1-5]
                    //jsonObject.put("score",serviceID);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String JSON = jsonObject.toString();
                //发送评论
                sendacomment(JSON);
            }

        });

    }

    private void sendacomment(String JSON) {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice/evaluate",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean)obj;
                                if(leavecommentBean.getCode()==200){
                                    Toast.makeText(ServicecommenttActivity.this,"发表成功",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },JSON
        );
    }

    private void but_bloak() {
        but_topchoicelawyy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ServicecommenttActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        serviceID = intent.getStringExtra("serviceID");
        Log.e(TAG,serviceID);
        but_topchoicelawyy = findViewById(R.id.but_topchoicelawyy);
        ratingBar = findViewById(R.id.rb);

        ed_servicecommentt = findViewById(R.id.ed_Servicecommentt);
        li_submit = findViewById(R.id.li_submit);
    }
}